﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Habi.Solver.ViewModels;
using DevExpress.Utils.MVVM.UI;
using DevExpress.XtraEditors;
using Habi.Solver.ViewModels.Resources;
using DevExpress.XtraEditors.Controls;
using DevExpress.Mvvm;
using Habi.Solver.Models.Resources;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.Data;
using Habi.Solver.Common;
using Habi.Solver.Services;
using DotNetExtensions.AspNet.DevExpress.Services;

namespace Habi.Solver.Views.Resources
{
    [ViewType(MainViewModel.ResourceTaskView)]
    public partial class ResourceTaskView : XtraUserControl
    {
        private readonly IGridViewService gridViewService;
        public ResourceTaskView()
        {
            InitializeComponent();
            gridViewService = new GridViewService();
            mvvmContext.ViewModelType = typeof(ResourceTaskViewModel);
            mvvmContext.RegisterService(new DialogMessageBoxService(this));
            if (!mvvmContext.IsDesignMode) InitializeView();
        }

        void InitializeView()
        {
            var fluent = mvvmContext.OfType<ResourceTaskViewModel>();
            int index = 0;
            repositoryItemImageComboBox1.Items.AddRange(svgImageCollectionIcons.Keys.Select(k => new ImageComboBoxItem { Value = k, ImageIndex = index++ }).ToArray());

            fluent.WithEvent<GridView, SelectionChangedEventArgs>(bandedGridView1, "SelectionChanged")
             .SetBinding(x => x.Selected, e =>
             {
                 int[] rows = bandedGridView1.GetSelectedRows();
                 List<ResourceTaskModel> tasks = new List<ResourceTaskModel>();
                 foreach (var rowHandle in rows)
                 {
                     tasks.Add(bandedGridView1.GetRow(rowHandle) as ResourceTaskModel);
                 }
                 return tasks;
             }, (grid, task) => { });

            fluent.WithEvent(this, "Load").EventToCommand(x => x.Load());
            fluent.SetBinding(gridControl, lb => lb.DataSource, x => x.Tasks);
            fluent.BindCommand(simpleButtonClose, x => x.Close());
            fluent.BindCommand(simpleButtonStart, x => x.Start());
            fluent.BindCommand(simpleButtonPasue, x => x.Pause());
            fluent.BindCommand(simpleButtonDel, x => x.Remove());

            TaskContainer.Default.OnTaskProgressChanging += TaskChanged;
            TaskContainer.Default.OnTaskCompleted += TaskChanged;
            TaskContainer.Default.OnTaskChanged += TaskChanged;
            gridViewService.Indicator<ResourceTaskModel>(gridControl);
        }

        private void TaskChanged(ResourceTaskModel e)
        {
            bandedGridView1.RefreshData();
        }

        private void TaskChanged(TaskEventArgs e)
        {
            bandedGridView1.RefreshData();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            // gridView.Columns["Progress"].ColumnEdit = repositoryItemProgressBar1;
            // bandedGridView1.Columns["Type"].ColumnEdit = repositoryItemImageComboBoxType;
            //gridView.Columns["Progress"].ImageOptions.Alignment = StringAlignment.Near;
            //gridView.Columns["Progress"].ImageOptions.Alignment = StringAlignment.Near;
        }
    }
}
